<?php
// @formatter:off
/**
 * @file WCRss.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-07-11 21:02:48 +0000 (Thu, 11 Jul 2013) $
 *
 * $Id: WCRss.php 95 2013-07-11 21:02:48Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/branches/wcomix-1.0/includes/WCRss.php $
 */
// @formatter:on

require_once "{$wcPaths["includes-directory"]}/WCRssItem.php";

/**
 * @class WCRss This class represents a RSS report and holds the logic to build a
 * RSS-XML from it.
 */
class WCRss {
	/**
	 * @var string This property holds the date format to be used on the XML.
	 */
	protected $_dateFormat;
	/**
	 * @var WCRssItem[] List of elements to be included when the XML is built.
	 */
	protected $_items = array();
	/**
	 * @var string[string] RSS properties to be added in the XML
	 */
	protected $_values = array(
		"title" => "",
		"description" => "",
		"link" => "",
		"lastBuildDate" => "",
		"pubDate" => "",
		"ttl" => ""
	);
	/**
	 * Class constructor.
	 */
	public function __construct() {
		//
		// Setting the last build date to now.
		$this->lastBuildDate = time();
		//
		// Setting the publishing date to now.
		$this->pubDate = time();
		//
		// Setting date format to it's default.
		$this->setDateFormat();
	}
	//
	// Public methods.
	/**
	 * This pseudo-method allows to get any of the internal properties stored
	 * in $_values.
	 *
	 * @param string $name Name of the property to get.
	 * @return string Returns the value of the requested property.
	 */
	public function __get($name) {
		//
		// Checking that the property is a valid one, otherwise it
		// triggers an error.
		if(!isset($this->_values[$name])) {
			trigger_error(__CLASS__.": Property '{$name}' not defined", E_USER_ERROR);
		}
		//
		// Returning property's value.
		return $this->_values[$name];
	}
	/**
	 * This pseudo-method allows to set any of the internal properties stored
	 * in $_values with a value.
	 *
	 * @param string $name Name of the property to set.
	 * @param string $value Value to associate with the property.
	 * @return string Returns the value of the updated property.
	 */
	public function __set($name, $value) {
		//
		// Checking that the property is a valid one, otherwise it
		// triggers an error.
		if(!isset($this->_values[$name])) {
			trigger_error(__CLASS__.": Property '{$name}' not defined", E_USER_ERROR);
		}
		//
		// Setting property's value.
		$this->_values[$name] = $value;
		//
		// Returning property's value.
		return $this->$name;
	}
	/**
	 * This method adds an RSS item to be shown in the XML.
	 *
	 * @param WCRssItem $item RSS item to be added.
	 */
	public function addItem(WCRssItem $item) {
		$this->_items[] = $item;
	}
	/**
	 * This method restart all internal setting of this object.
	 */
	public function reset() {
		//
		// All items are dropped.
		$this->_items = array();
		//
		// Setting every internal property as empty.
		foreach($this->_values as $property => $value) {
			$this->_values[$property] = "";
		}
		//
		// Setting date format to it's default.
		$this->setDateFormat();
	}
	/**
	 * This method allows to change the date format to use in RSS-XMLs.
	 *
	 * @param string $format Format to be set. When this parameter is not
	 * given it assumes "r".
	 */
	public function setDateFormat($format = "r") {
		$this->_dateFormat = $format;
	}
	/**
	 * This method builds and returns the RSS in XML format.
	 *
	 * @return string Returns the built XML from current object setting.
	 */
	public function xml() {
		//
		// Setting the default value to be returned.
		$out = "";
		//
		// Adding the general XML tag.
		$out .= "<?xml version=\"1.0\"?>";
		//
		// Starting the RSS.
		$out .= "<rss version=\"2.0\">";
		//
		// Starting a channel data.
		$out .= "<channel>";
		//
		// Setting a channel's title.
		$out .= "<title>".WCRSS::Satinaze($this->title)."</title>";
		//
		// Giving a channel's description.
		$out .= "<description>".WCRSS::Satinaze($this->description)."</description>";
		//
		// Giving a main like for the channel.
		$out .= "<link>".WCRSS::Satinaze($this->link)."</link>";
		//
		// Setting the publishing date.
		$out .= "<pubDate>".WCRSS::Satinaze(date($this->_dateFormat, $this->pubDate))."</pubDate>";
		//
		// Adding all element's XML texts.
		foreach($this->_items as $item) {
			//
			// Requesting each element in XML format.
			$out .= $item->xml();
		}
		//
		// Closing channel.
		$out .= "</channel>";
		//
		// Closing the RSS.
		$out .= "</rss>";
		//
		// Returning built XML.
		return $out;
	}
	//
	// Public class methods.
	/**
	 * This class method takes a text with HTML tags and returns it ready to
	 * be printed with escape characters.
	 *
	 * @param string $value HTML text to clean.
	 * @return string Returns a clean string.
	 */
	public static function Satinaze($value) {
		return htmlentities($value);
	}
}
?>